package com.alander.nexusgo.post.controller;

import com.alander.nexusgo.common.model.dto.PostLikesListDTO;
import com.alander.nexusgo.common.model.vo.PostLikesVO;
import com.alander.nexusgo.common.response.Result;
import com.alander.nexusgo.framework.web.starter.controller.BaseController;
import com.alander.nexusgo.post.service.PostLikesService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.getPageInfo;
import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.startPage;

/**
* 帖子、评论点赞管理
*/
@RestController
@RequestMapping("/api/likes")
public class PostLikesController extends BaseController {
    
    @Autowired
    private PostLikesService postLikesService;
    
    /**
     * 点赞/取消点赞
     */
    @PostMapping
    public Result<String> like(
            @RequestParam(value = "targetId") Long targetId,
            @RequestParam(value = "targetType") Integer targetType) {
        try {
            String result = postLikesService.like(targetId, targetType, getUserId());
            return success(result);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
    
    /**
     * 检查是否已点赞
     */
    @GetMapping("/check")
    public Result<Boolean> checkLike(
            @RequestParam(value = "targetId") Long targetId,
            @RequestParam(value = "targetType") Integer targetType) {
        try {
            boolean result = postLikesService.checkLike(targetId, targetType, getUserId());
            return success(result);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
    
    /**
     * 获取点赞详情
     */
    @GetMapping("/{id}")
    public Result<PostLikesVO> get(@PathVariable(value = "id") Long id) {
        PostLikesVO like = postLikesService.getLikeById(id);
        if (like == null) {
            return fail("点赞记录不存在");
        }
        return success(like);
    }
    
    /**
     * 分页查询点赞列表
     */
    @GetMapping
    public PageInfo<PostLikesVO> list(@RequestBody PostLikesListDTO postLikesListDTO) {
        startPage(postLikesListDTO);
        List<PostLikesVO> likesVOList = postLikesService.listLikes(postLikesListDTO);
        return getPageInfo(likesVOList);
    }
}